package com.elite;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;

import com.novell.ldap.LDAPAttribute;
import com.novell.ldap.LDAPAttributeSet;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPEntry;
import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPSearchConstraints;
import com.novell.ldap.LDAPSearchResults;



public class LDAP implements Runnable{
	private static final Log log=LogFactory.getLog(LDAP.class);
	public static com.elite.FtpBean ftpbean;
	public static String times;
	private boolean flag = true; 
	private static int Hz =1; 
	public static String username;
	public static String username1;
	public static String password;
	public static String password1;
	public static String XHH;
	public static String XHH1;
	private static String IP="";
	private static String PORT="";
	private static String Filter="";
	private static String OU="";
	private static String OU1="";
	private static String DC="";
	private static String daytime="";
	private static String time_flag="";
	private static String SQL_NAME="";

	public static String getOU1() {
		return OU1;
	}
	public static void setOU1(String ou1) {
		OU1 = ou1;
	}
	public static String getSQL_NAME() {
		return SQL_NAME;
	}
	public static void setSQL_NAME(String sql_name) {
		SQL_NAME = sql_name;
	}
	public static String getTime_flag() {
		return time_flag;
	}
	public static void setTime_flag(String time_flag) {
		LDAP.time_flag = time_flag;
	}
	public static String getDC() {
		return DC;
	}
	public static void setDC(String dc) {
		DC = dc;
	}
	public static String getOU() {
		return OU;
	}
	public static void setOU(String ou) {
		OU = ou;
	}
	public static String getFilter() {
		return Filter;
	}
	public static void setFilter(String filter) {
		Filter = filter;
	}
	public static String getPORT() {
		return PORT;
	}
	public static void setPORT(String port) {
		PORT = port;
	}
	public static String getIP() {
		return IP;
	}
	public static void setIP(String ip) {
		IP = ip;
	}
	public static com.elite.FtpBean getFtpbean() {
		return ftpbean;
	}
	public static void setFtpbean(com.elite.FtpBean ftpbean) {
		LDAP.ftpbean = ftpbean;
	}
	public static String getTimes() {
		return times;
	}
	public static void setTimes(String times) {
		LDAP.times = times;
	}
	public static String getUsername() {
		return username;
	}
	public static void setUsername(String username) {
		LDAP.username = username;
	}
	public static String getUsername1() {
		return username1;
	}
	public static void setUsername1(String username1) {
		LDAP.username1 = username1;
	}
	public static String getPassword() {
		return password;
	}
	public static void setPassword(String password) {
		LDAP.password = password;
	}
	public static String getPassword1() {
		return password1;
	}
	public static void setPassword1(String password1) {
		LDAP.password1 = password1;
	}
	public static String getXHH() {
		return XHH;
	}
	public static void setXHH(String xhh) {
		XHH = xhh;
	}
	public static String getXHH1() {
		return XHH1;
	}
	public static void setXHH1(String xhh1) {
		XHH1 = xhh1;
	}
	public void run2(String a) throws LDAPException{
		LDAPConnection lc = new LDAPConnection(); 
		log.info("IP:"+IP);
		log.info("PORT:"+PORT);
		lc.connect(IP,Integer.parseInt(PORT));   
        log.info("username:"+username);
        log.info("password:"+password);
        lc.bind(LDAPConnection.LDAP_V3,username,password);   
        log.info("Filter:"+Filter);
        LDAPSearchConstraints constraints=new LDAPSearchConstraints();
//        log.info("constraints.getMaxResults():"+constraints.getMaxResults());
        constraints.setMaxResults(3000);  
        LDAPSearchResults rs=lc.search(a,LDAPConnection.SCOPE_SUB,Filter,null,false,constraints);  
        log.info("rs.getCount():"+rs.getCount());
        int count=0;   
        while(rs.hasMore()){  
        	try{
        		log.info("------S---------");
        		LDAPEntry entry=rs.next();
        		if(entry!=null){
        			String sAMAccountName=getVAlue(entry.getAttribute("sAMAccountName"));
        			String name=getVAlue(entry.getAttribute("name"));
        			String company=getVAlue(entry.getAttribute("company"));
        			String department=getVAlue(entry.getAttribute("department"));
        			String mail=getVAlue(entry.getAttribute("mail"));
        			log.info(entry.getDN()); 
                    log.info(sAMAccountName); 
                    log.info(company);   
                    log.info(name);  
                    log.info(department); 
                    log.info(mail);   
                    String result=ftpbean.test2(SQL_NAME,sAMAccountName,name,company,department,mail,"");
                    log.info("result:"+result);
        		}
        		log.info("-------E---------");
        		count++; 
        	}catch(Exception e){
        		 log.error("过程异常:"+getERRO(e));   
        	}  
        }
        log.info("count:"+count);
        lc.disconnect();
	}
	public String getVAlue(LDAPAttribute a){
		if(a==null){
			log.info(a);
			return "";
		}else{
			return a.getStringValue();
		}
		
	}
	public void run1(String[] OUS){
		if(true){
			try{
				for(int i=0;i<OUS.length;i++){
					if(OUS[i].equals("分公司或筹备组")){
						log.info("OU1:"+OU1);
						String[] OU2=OU1.split(",");
						if(OU2!=null&&OU2.length>0){
							for(int ii=0;ii<OU2.length;ii++){
								String URL="OU="+OU2[ii]+",OU="+OUS[i]+","+DC;
								log.info("URL:"+URL);
								run2(URL);
							}
						}else{
							String URL="OU="+OUS[i]+","+DC;
							log.info("URL:"+URL);
							run2(URL);
						}
					}else{
						String URL="OU="+OUS[i]+","+DC;
						log.info("URL:"+URL);
						run2(URL);
					}
				}
			}catch(Exception e){
				log.error("run1异常:"+getERRO(e)); 
			}
		}
	}
	public void run(){ 
	       try {   
	    	   Hz=Integer.parseInt(times);
	    	   log.info("Hz:"+Hz);
	    	   log.info("OU:"+OU);
	    	   if(OU!=null&&OU.length()>0){
	    		   String[] OUS=OU.split(",");
	    		   if(OUS!=null&&OUS.length>0){
	    			   while(flag){
	    				   Date date=new Date();
	    				   String ee=String.format("%tR",date); //获取现在几点
	    				   String dd=String.format("%tF",date); //获取现在几号
	    				   String now1=ee.substring(0,2);
	    				   log.info("dd:"+dd);
	    				   log.info("daytime:"+daytime);
	    				   log.info("now1:"+now1);
	    				   log.info("time_flag:"+time_flag);
	    				   if(now1.equals(time_flag)&&!dd.equals(daytime)){
	    					   log.info("------------------StartRun()----------------------");
	    					   run1(OUS);
	    					   daytime=dd;
	    					   log.info("-------------------------同步完成--------------------------");
	    				   }else{
	    					   log.info("------------------条件未到不同步,进程休眠"+Hz+"秒----------------------");
	    				   }
	    				   try{
	    						Thread.sleep(Hz*1000); 
	    				   }catch(Exception e){
	    						log.error("休眠异常:"+getERRO(e)); 
	    				   }
	    			   }
	    		   }
	    	   }
	    	   
//	    	   log.info("-------公司领导信息-------");
//	    	   run1("OU=公司领导,DC=hlic,DC=cn");
//	    	   log.info("-------分公司或筹备组信息-------");
//	    	   run1("OU=分公司或筹备组,DC=hlic,DC=cn");
//	    	   log.info("-------总公司部门信息-------");
//	    	   run2("OU=总公司部门,DC=hlic,DC=cn");
	       } catch (Exception e) {   
	    	   log.error("连接异常:"+getERRO(e));   
	            
	       }  
	}
	public static String getERRO(Exception e){
		String str="";
		try{
            StringWriter sw = new StringWriter();  
            e.printStackTrace(new PrintWriter(sw, true));  
            str = sw.toString(); 	
		}catch(Exception e1){
			str=e.toString();
			log.error("getERRO:"+e1.toString());
		}
		return str;
	}        
	

	

}





















